var ctx;
var name = "";
var pSize;

function setCtx(ctx) {
	this.ctx = ctx;
}
function gotoPage(page) {
	name = ifNull($("#name").val());
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if (name == $("#name").attr("placeholder")) {
		name = "";
	}

	loadContent("/shopInfo/shopAppliacation?pNo=" + page + "&name=" + name
			+ "&pSize=" + pSize);
}

function ifNull(mess) {
	if (mess == null || mess == undefined) {
		mess = "";
	}

	return mess;
}

//条件搜索用户列表
function getShopInfoList() {
	var approveState = $("#approveState").val();
	name = ifNull($("#name").val());
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if (name == $("#name").attr("placeholder")) {
		name = "";
	}

	loadContent("/shopInfo/shopAppliacation?pNo=1&name=" + name + "&pSize="
			+ pSize + "&applyState=" + approveState);

}

function gotoShopInfoPage(page) {
	if (page == null) {
		page = $(".inputclass").val();
	}
	name = ifNull($("#name").val());
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if (name == $("#name").attr("placeholder")) {
		name = "";
	}
	loadContent("/shopInfo/shopAppliacation?pNo=" + page + "&name=" + name
			+ "&pSize=" + pSize);
}

//全选和反选
function selectAll(target) {
	if (target.checked) {
		$("#tableTbody :checkbox").attr("checked", true);
		$(target).parents("thead").siblings("tbody").children().children()
				.each(function() {
					$(this).css("background", "#D3F0F1");
				});
	} else {
		$("#tableTbody :checkbox").attr("checked", false);
		$(target).parents("thead").siblings("tbody").children().children()
				.each(function() {
					$(this).css("background", "");
				});
	}
}

//鼠标滑入一行改变背景颜色
function mouseoverOne(target) {
	$(target).children().each(function() {
		$(this).css("background", "#D3F0F1");
	});
}

//鼠标滑出一行恢复原来的背景颜色
function mouseoutOne(target) {
	$(target).children().each(function() {
		$(this).css("background", "");
	});
}

//选择或取消选中当前记录
function selectOne(id, target) {
	if (target.checked) {
		$("#shop" + id).attr("checked", true);
		$(target).parents("tr").children().each(function() {
			$(this).css("background", "#D3F0F1");
		});
	} else {
		$("#shop" + id).attr("checked", false);
		$(target).parents("tr").children().each(function() {
			$(this).css("background", "");
		});
	}
}

function detailshopInfo(id) {
	loadContent("/shopInfo/shopInfoDetail?id=" + id);
}

function approved(id, state) {
	var message;
	switch (parseInt(state)) {
	case 1:
		message = "是否通过审核？";
		break;
	case 2:
		message = "是否禁用？";
		break;
	case 3:
		message = "是否启用？";
		break;
	default:
		break;
	}
	layer.confirm(message, {
		icon : 3,
		title : '提示'
	}, function(index) {
		$.ajax({
			type : "POST",
			url : ctx + "/shopInfo/shopInfoUpdate",
			cache : false,
			dataType : 'json',
			data : {
				id : id,
				state : state
			},
			success : function(result) {
				if (result.success) {
					layer.msg("操作成功", {
						icon : 1,
						time : 1000
					// 2秒关闭（如果不配置，默认是3秒）
					}, function() {
						loadContent("/shopInfo/shopInfoList");
					});
				} else {
					layer.msg("操作失败", {
						icon : 2
					});
				}
			}
		});
		layer.close(index);
	});
}

function deleteByIds() {
	if ($("img[name='cellChecked'][src*=honggou]").length == 0) {
		layer.msg("请选择要删除的记录!", {
			icon : 3,
			time : 2000
		//2秒关闭（如果不配置，默认是3秒）
		});
	} else {
		var delIds = [];
		$("#tableTbody img[name='cellChecked'][src*=honggou]").each(function() {
			delIds.push($(this).attr("class"));
		});
		var vals = delIds.join(',');//转换为逗号隔开的字符串 
		layer.confirm('确认删除?', {
			icon : 3,
			title : '提示'
		}, function(index) {
			//do something
			$.post(ctx + "/shopInfo/shopInfoList", {
				cellIDs : vals
			}, function(data) {
				data = JSON.parse(data);
				if (data.success) {
					layer.msg("删除成功", {
						icon : 1,
						time : 2000
					//2秒关闭（如果不配置，默认是3秒）
					}, function() {
						loadContent("/shopInfo/shopInfoList");
					});
				} else {
					layer.msg(data.msg, {
						icon : 2,
						time : 2000
					//2秒关闭（如果不配置，默认是3秒）
					});
				}
			});
			layer.close(index);
		});

	}
}

//实现字段较长时的隐藏于显示
var index;
function mouseOverAddr(item, addr) {
	index = layer.tips(addr, '#addr' + item, {
		tips : [ 3, '#37CED4' ],
		time : 5000
	});
}
function mouseOutAddr(item, addr) {
	layer.close(index);
}

function updateSort(shopInfoId) {
	if (shopInfoId != null) {
		$.get(ctx + "/shopInfo/updateSort", {
			id : shopInfoId
		}, function(data) {
			data = JSON.parse(data);
			if (data.success) {
				layer.msg("操作成功", {
					icon : 1,
					time : 2000
				//2秒关闭（如果不配置，默认是3秒）
				}, function() {
					loadContent("/shopInfo/shopInfoList");
				});
			} else {
				layer.msg(data.msg, {
					icon : 2,
					time : 2000
				//2秒关闭（如果不配置，默认是3秒）
				});
			}
		});
	}
}

function approve(id, state) {
	var cellId = $("#cellId").val();
	var message;
	switch (parseInt(state)) {
	case 2:
		message = "是否予以通过审核？";
		break;
	case 5:
		message = "是否不予以通过审核？";
		break;
	default:
		break;
	}
	layer.confirm(message, {
		icon : 3,
		title : '提示'
	}, function(index) {
		$.ajax({
			type : "POST",
			url : ctx + "/shopInfo/shopInfoUpdating",
			cache : false,
			dataType : 'json',
			data : {
				id : id,
				state : state,
				cellId : cellId
			},
			success : function(result) {
				if (result.success) {
					layer.msg("操作成功", {
						icon : 1,
						time : 1000
					// 2秒关闭（如果不配置，默认是3秒）
					}, function() {
						loadContent("/shopInfo/shopAppliacation");
						if (result.obj != 0) {
							$('#unRead').show();
							$('#unRead').html(result.obj);
						} else {
							$('#unRead').hide();
						}
					});
				} else {
					layer.msg("操作失败", {
						icon : 2
					});
				}
			}
		});
		layer.close(index);
	});
}

function self(id, selfstore) {
	var message;
	switch (parseInt(selfstore)) {
	case 1:
		message = "是否设定为非自营店？";
		break;
	case 0:
		message = "是否设定为自营店？";
		break;
	default:
		break;
	}
	layer.confirm(message, {
		icon : 3,
		title : '提示'
	}, function(index) {
		$.ajax({
			type : "POST",
			url : ctx + "/shopInfo/selfStore",
			cache : false,
			dataType : 'json',
			data : {
				id : id,
				selfstore : selfstore
			},
			success : function(result) {
				if (result.success) {
					layer.msg("操作成功", {
						icon : 1,
						time : 1000
					// 2秒关闭（如果不配置，默认是3秒）
					}, function() {
						loadContent("/shopInfo/shopInfoList");
					});
				} else {
					layer.msg("操作失败", {
						icon : 2
					});
				}
			}
		});
		layer.close(index);
	});
}

function showDetail(id) {
	loadContent("/shopInfo/showDetail?id="+id);
}

// 新增备注
function addMemo(id) {
    if (null == id || "" == id || undefined == id) {
        layer.open("该商户的店铺不存在，不能添加商户审核备注信息！！！", {
            icon : 2,
            time : 2000
        // 2秒关闭（如果不配置，默认是3秒）
        });
    } else {
        msg = "添加商户申请备注";
        url = ctx + "/shopInfo/addMemo?id=" + id;
        saveOrUpdateMemo(msg, url, id);
    }
}

// 保存备注信息
var index = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
function saveOrUpdateMemo(msg, url, id) {
    // 官网欢迎页
    index = layer.open({
        type : 2,
        // skin: 'layui-layer-lan',
        title : msg,
        fix : false,
        shadeClose : true,
        scrollbar : false,
        maxmin : false,
        edit : true,
        closeBtn : 1,
        area : [ '500px', '230px' ],
        content : url,
        btn : [ '确定', '取消' ],
        yes : function(index, layero) { // 或者使用btn1
            // 按钮【按钮一】的回调
            var iframeWin = window[layero.find('iframe')[0]['name']];
            iframeWin.saveMemo(id);
        },
        cancel : function(index) { //或者使用btn2
            //按钮【按钮二】的回调
        }
    });
}
